package com.zdsoft.blp.zongbuju.shouye.linliquan.activity;

import android.content.Intent;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;


import com.squareup.picasso.Picasso;
import com.zdsoft.blp.R;
import com.zdsoft.blp.gongyong.API.API;
import com.zdsoft.blp.gongyong.fanhui.FanHuiView;
import com.zdsoft.blp.gongyong.imageFrame.IvFrameView;
import com.zdsoft.blp.gongyong.imageTrans.CircleImage;
import com.zdsoft.blp.gongyong.net.BaseActivity;
import com.zdsoft.blp.gongyong.net.Net;
import com.zdsoft.blp.gongyong.net.NetCallback;
import com.zdsoft.blp.zongbuju.shouye.linliquan.adapter.LLPingLunAdapter;
import com.zdsoft.blp.zongbuju.shouye.linliquan.entity.LinLi;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/9/1.
 */
public class XQPCActivity extends BaseActivity implements NetCallback {
    private ListView lv;
    private TextView pc_name,pc_title_time,pc_qidian,pc_pinglun,pc_zhongdian,pc_time,pc_lianxiren,pc_dianhua,pc_cheliang,pc_miaoshu;
    private ImageView pc_iv,pinche_pinglun;
    private IvFrameView pc_content_image;
    private Intent intent;
    private List<LinLi> list;
    private FanHuiView pinche_title;
    private String huifuTime,touxiang,huifuUser,huifuImage,huifuContent;


    @Override
    protected void init() {
        setContentView(R.layout.linli_pinche_xiangqing);
//        实例化控件
        lv = find(R.id.pinche_xlv);
        lv.setFocusable(false);
        pc_name = find(R.id.pc_name);
        pc_title_time = find(R.id.pc_title_time);
        pc_qidian = find(R.id.pc_qidian);
        pc_zhongdian = find(R.id.pc_zhongdian);
        pc_time = find(R.id.pc_time);
        pc_lianxiren = find(R.id.pc_lianxiren);
        pc_dianhua = find(R.id.pc_dianhua);
        pc_cheliang = find(R.id.pc_cheliang);
        pc_miaoshu = find(R.id.pc_miaoshu);
        pc_pinglun = find(R.id.pc_pinglun);
        pinche_pinglun = find(R.id.pinche_pinglun);
        pc_iv = find(R.id.pc_iv);
        pinche_title = find(R.id.pinche_title);
        pinche_title.getTv_title().setText("拼车详情");
        pc_content_image = find(R.id.pc_content_image);
//        接收Intent传值
        intent = getIntent();
        final String id=intent.getStringExtra("id");
//        设置Listview不能滑动的
        lv.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return true;
            }
        });
//        实例化Net
        Net net=new Net();
        net.url= API.CARPOOLINGINFO_URL;
        net.nameSpace=API.CARPOOLINGINFO_NAMESPACE;
        net.methodName=API.CARPOOLINGINFOCONTENT;
        Map<String,Object> map=new HashMap();
        map.put("id",id);
        getServer(net,this);
        net.parms=map;
        //跳转 评论界面
        pinche_pinglun.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                intent=new Intent(XQPCActivity.this,LLHFActivity.class);
                intent.putExtra("id","2");
                startActivity(intent);
            }
        });
    }

    //回调函数
    public void onResponse(boolean flag, int code, String res) {
        Log.e("RESRES",res);
        try {
            JSONObject object=new JSONObject(res);
            pc_name.setText(object.getString("userName")); //用户名
            touxiang=object.getString("headimgurl");//用户头像
            Picasso.with(this).load(touxiang).transform(new CircleImage()).into(pc_iv);
            pc_time.setText(object.getString("startTime"));//发车时间
            pc_title_time.setText(object.getString("createTime"));//发布时间
            pc_dianhua.setText(object.getString("contactPhone"));//联系电话
            pc_lianxiren.setText(object.getString("contact"));//联系人
            pc_miaoshu.setText(object.getString("content"));//描述
            pc_qidian.setText(object.getString("start"));//开始地点
            pc_cheliang.setText(object.getString("carInfo"));//车型
            pc_zhongdian.setText(object.getString("end"));//终点
            pc_pinglun.setText(object.getInt("replyNum")+"");//评论人数
            boolean image=object.isNull("picPathSet");//content 图片
            if (image==false){
                JSONArray array=object.getJSONArray("picPathSet");
                for (int i=0;i<array.length();i++){
                    JSONObject obj=array.getJSONObject(i);
                    String contentimage=obj.getString("picPath");
                    if (!"".equals(contentimage)){
                        pc_content_image.setVisibility(View.VISIBLE);
                        pc_content_image.show(new String[]{contentimage});
                    }else {
                        pc_content_image.setVisibility(View.GONE);
                    }
                }
            }
//            解析回复
            boolean replySet=object.isNull("replySet");
            if (replySet==false){
                list=new ArrayList<>();
                LinLi pinche=null;
                JSONArray pingL=object.getJSONArray("replySet");
                for (int i=0;i<pingL.length();i++){
                    JSONObject pc_huifu=pingL.getJSONObject(i);
                    huifuUser=pc_huifu.getString("replyUserName");
                    huifuTime=pc_huifu.getString("createTime");
                    huifuContent=pc_huifu.getString("content");
                    huifuImage=pc_huifu.getString("replyUserHeadimgurl");
                    pinche=new LinLi(huifuUser,huifuContent,huifuImage,huifuTime);
                    list.add(pinche);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        //listview 展示
        LLPingLunAdapter adapter = new LLPingLunAdapter(list, getApplicationContext());
        lv.setAdapter(adapter);
    }
}
